#include "main.h"

//LED
uchar led_warn;
uchar led_code;

//SEG
uchar seg_pos;
uchar seg_str[12];
uchar seg_buf[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
uchar seg_mode;

//KEY
uchar key_old;

//TIMER
ulong timer_count;

//SONIC
uchar sonic_val = 30;

//ADC
float adc_val;
uchar adc_set_mode = 3;

//参数
bit ctrl_mode;
uchar top_val = 60;
uchar down_val = 10;
bit warn_en;
uchar warn_val;


//===========TASK=============
void task_proc() {
	if(timer_count%TASK_TIME) return;
	
	//距离报警检测
	if(sonic_val < down_val || sonic_val > top_val) {
		if(~warn_en)
			warn_val++;
		warn_en = 1;
	} else {
		warn_en = 0;
	}
	
	//滑变调节
	if(ctrl_mode) {
		switch(adc_set_mode) {
			case 1://下限
				if((uint)adc_val < 5)
					down_val = ((uint)adc_val)*10;
				else
					down_val = 40;
			break;
			case 2://上限
				if((uint)adc_val < 5)
					top_val = ((uint)adc_val)*10 + 50;
				else
					top_val = 90;
			break;
		}	
	}
	
}

//===========DATA=============
void data_proc() {
	if(timer_count%DATA_TIME) return;
	sonic_val = sonic_read();
	if(ctrl_mode)
		adc_val = ADC_read(3)*(5.0/255);
}

//===========LED=============
void led_proc() {
	if(timer_count%LED_TIME) return;
	led_code = 0x01 << seg_mode;
	if(warn_en)
		led_code |= led_warn;
	else
		led_code |= 0x80;
}

//===========SEG=============
void seg_proc() {
	if(timer_count%SEG_TIME) return;
	
	switch(seg_mode) {
		case 0:  //测距
			sprintf(seg_str, "A    %3d", (uint)sonic_val);
		break;
		case 1:  //参数
			if(~ctrl_mode)  //按键
				sprintf(seg_str, "P1 %02d-%02d", (uint)down_val, (uint)top_val);
			else  //滑变
				sprintf(seg_str, "P2 %02d-%02d", (uint)down_val, (uint)top_val);
		break;
		case 2:  //记录
			if(warn_val <= 9)
				sprintf(seg_str, "E      %d", (uint)warn_val);
			else 
				sprintf(seg_str, "E      -");
		break;
	}
	
	seg_tran(seg_str, seg_buf);
}

//===========KEY=============
void key_proc() {
	uchar key_val,key_down;
	if(timer_count%KEY_TIME) return;
	
	//读取键码
	key_val = key_scan_kbd();
	key_down = key_val & (key_old^key_val);
	key_old = key_val;
	
	switch(key_down) {
		case 4: //界面切换、电位器调节取消使能
			if(++seg_mode == SEG_MODE)
				seg_mode = 0;
			adc_set_mode = 3;
		break;
		case 5: //控制模式切换、报警数清零
			if(seg_mode == 1)
				ctrl_mode ^= 1;
			else if(seg_mode == 2)
				warn_val = 0;
		break;
		case 8: //调整下限
			if(seg_mode == 1) {
				if(~ctrl_mode) { //按键
					if(down_val < 40)
						down_val += 10;
					else 
						down_val = 0;
				} else { //滑变
					adc_set_mode = 1;
				}
			}	
		break;
		case 9: //调整上限
			if(seg_mode == 1) {
				if(~ctrl_mode) {
					if(top_val < 90)
						top_val += 10;
					else 
						top_val = 50;
				} else {
					adc_set_mode = 2;
				}
			}
		break;
	}
	
}

//===========ISR=============
void timer1_init() {
	AUXR &= 0xBF;
	TMOD &= 0x0F;
	TH1 = 0xFC;  //1ms
	TL1 = 0x18;
	TF1 = 0;
	ET1 = 1;
	TR1 = 1;
	EA = 1;
}

void isr_timer_1() interrupt 3 {
	timer_count++;
	
	//L8每0.1s闪烁一次
	if(warn_en)
		if(!(timer_count%100))
			led_warn ^= 0x80;
	
	led_disp(led_code);
	seg_disp(seg_pos, seg_buf);
	if(++seg_pos == 8) seg_pos = 0;
}

//===========MAIN=============
void main() {
	sys_init();
	timer1_init();
	
	while(1) {
		led_proc();
		seg_proc();
		key_proc();
		data_proc();
		task_proc();
	}
}


